const app = getApp();

Page({
  data: {
    customerRequest: null,
    ingredients: app.globalData.ingredients,
    cookware: app.globalData.cookware,
    draggingIngredient: null,
    dragX: 0,
    dragY: 0
  },

  onLoad() {
    this.generateCustomerRequest();
  },

  generateCustomerRequest() {
    const randomIngredient = this.data.ingredients[Math.floor(Math.random() * this.data.ingredients.length)];
    const randomCookware = this.data.cookware[Math.floor(Math.random() * this.data.cookware.length)];
    this.setData({
      customerRequest: {
        ingredient: randomIngredient,
        cookware: randomCookware
      }
    });
  },

  onTouchStart(e) {
    const startX = e.touches[0].clientX;
    const startY = e.touches[0].clientY;
    this.setData({
      draggingIngredient: e.currentTarget.dataset.ingredient,
      startX,
      startY
    });
  },

  onTouchMove(e) {
    const currentX = e.touches[0].clientX;
    const currentY = e.touches[0].clientY;
    const deltaX = currentX - this.data.startX;
    const deltaY = currentY - this.data.startY;
    this.setData({
      dragX: deltaX,
      dragY: deltaY
    });
  },

  onTouchEnd(e) {
    // 简单判断是否拖动到正确的烹饪用具上，可根据实际调整坐标范围
    const { customerRequest, draggingIngredient } = this.data;
    if (draggingIngredient === customerRequest.ingredient) {
      // 模拟拖动到正确烹饪用具
      wx.showToast({ title: '制作成功' });
      this.generateCustomerRequest();
    } else {
      wx.showToast({ title: '制作失败', icon: 'none' });
    }
    this.setData({
      draggingIngredient: null,
      dragX: 0,
      dragY: 0
    });
  }
});